import pygame
from pygame.locals import *
from sys import exit

class Grid:
    
    board = {}
    
    def __init__(self, position, size, square):
        
        self.rows, self.cols = size
        self.sqr_width, self.sqr_height = square
        self.left, self.top = position

        
    def load(self):
        for i in range(0,self.rows):
            for j in range(0, self.cols):
                n_top = self.top + i*self.sqr_height
                n_left = self.left + j*self.sqr_width

                self.board[str(i)+'_'+str(j)] = Rect((n_left, n_top), (self.sqr_width,self.sqr_height))

    def get_position(self, point):
        col = (point[0]-self.left) / self.sqr_width
        row = (point[1]-self.top) / self.sqr_height
        
        if row >= self.rows or col >= self.cols or row < 0 or col < 0:
            return None
        
        return (row,col)
        
    def show(self, screen):
        color = (255,255,255)

        for rect in self.board.itervalues():
            pygame.draw.rect(screen, color, rect, 1)
    
    

if __name__ == "__main__":
    grid = Grid((5,2), (12,9), (10,10))
    
    grid.load()
    
    print grid.board["3_6"]
    print grid.board["3_7"]
    
    print grid.get_position((70,25))
    
    
